﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using fanfu.Common;
using fanfu.Model;
using System.Data.SqlClient;

namespace fanfu.Business
{
    public class ReviewRule
    {
        //----------后台调用------------

        #region 获取所有评论信息
        /// <summary>
        /// 获取所有评论信息
        /// </summary>
        public DataTable GetAllReviewInfo()
        {
            string sqlString = "SELECT * FROM Review ORDER BY ReviewTime ASC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 获取指定ID的评论信息
        /// <summary>
        /// 获取指定ID的评论信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ReviewModel GetReviewByID(string ID)
        {
            string sqlString = "SELECT TOP 1 * FROM Review WHERE ID=@ID";
            SqlParameter para = new SqlParameter("@ID", ID);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            if (dt.Rows.Count == 1)
                return new ReviewModel(dt.Rows[0]);
            else
                return null;
        }
        #endregion

        #region 删除指定ID的评论信息
        /// <summary>
        /// 删除指定ID的评论信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteReview(string ID)
        {
            string sqlString = string.Format("UPDATE Review SET Flag={0} WHERE ID={1}", (int)ReviewFlag.delete, ID);
            return SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        #region 评论状态枚举值转换
        /// <summary>
        /// 评论状态枚举值转换
        /// </summary>
        public string GetFlagDes(object objFlag)
        {
            string strResult = string.Empty;
            int flag = Convert.ToInt32(objFlag);
            switch (flag)
            {
                case (int)ReviewFlag.noRead: strResult = "未阅"; break;
                case (int)ReviewFlag.read: strResult = "已阅"; break;
                case (int)ReviewFlag.myAnswer: strResult = "我的回答"; break;
                case (int)ReviewFlag.delete: strResult = "删除"; break;
            }
            return strResult;
        }
        #endregion

        //----------前台调用------------

        #region 创建边栏最新评论HTML
        /// <summary>
        /// 创建边栏最新评论HTML
        /// </summary>
        /// <param name="topNum">需要创建的记录条数</param>
        /// <returns></returns>
        public StringBuilder CreateNewReviewHtml(int topNum)
        {
            string sendPage = string.Empty;
            string strFormat = string.Empty;
            StringBuilder newReview = new StringBuilder();
            string sqlString = "SELECT TOP " + topNum + " ID, ParentId, ParentName, ParentType, ReviewerName, ReviewContent FROM Review WHERE Flag<>" + (int)ReviewFlag.myAnswer + " AND Flag<>" + (int)ReviewFlag.delete + " ORDER BY ReviewTime DESC";
            DataTable dt= SqlHelper.GetDataTable(sqlString);
            foreach (DataRow dr in dt.Rows)
            {
                //判断评论的对象是文章还是图片
                int parentType = int.Parse(dr["ParentType"].ToString());
                if (parentType == (int)ReviewParentType.article)
                    sendPage = "Article.aspx?current=0&id=";
                else if (parentType == (int)ReviewParentType.image)
                {
                    //获取相片属于哪个相册
                    sqlString = "SELECT ParentId FROM UploadFile WHERE ID=" + dr["ParentId"].ToString();
                    sendPage = "PhotoAlone.aspx?current=2&group=" + SqlHelper.GetScalar(sqlString).ToString() + "&id=";
                }
                //替换Html数据
                strFormat = "<li><a title=\"{0}\" href=\"javascript:\">{1}</a> 在 <a title=\"{2}\" href=\"" + sendPage + "{3}\">{4}</a><br/>说: {5}</li>";
                newReview.AppendFormat(strFormat
                        , dr["ReviewerName"], StringPlus.StringCut(dr["ReviewerName"].ToString(), 8)
                        , dr["ParentName"], dr["ParentId"], StringPlus.StringCut(dr["ParentName"].ToString(), 16)
                        , StringPlus.StringDot(dr["ReviewContent"].ToString(), 28));
            }
            return newReview;
        }
        #endregion

        #region 根据BaseId,ParentType获取状态为[我的回复]的评论信息
        /// <summary>
        /// 根据BaseId,ParentType获取状态为[我的回复]的评论信息
        /// </summary>
        /// <param name="BaseId">评论ID</param>
        /// <param name="ParentType">评论的类型</param>
        /// <returns></returns>
        public DataTable GetReviewByBaseId(string BaseId, int ParentType)
        {
            string sqlString = "SELECT * FROM Review WHERE BaseId=@BaseId AND ParentType=" + ParentType + " AND Flag=" + (int)ReviewFlag.myAnswer;
            SqlParameter para = new SqlParameter("@BaseId", BaseId);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            return dt;
        }
        #endregion

        #region 根据ParentId,ParentType获取非删除状态的评论信息列表
        /// <summary>
        /// 根据ParentId,ParentType非删除状态的评论信息列表
        /// </summary>
        /// <param name="ParentId">文章或图片的ID</param>
        /// <param name="ParentType">评论的类型</param>
        /// <returns></returns>
        public DataTable GetReviewByParentId(string ParentId, int ParentType)
        {
            string sqlString = "SELECT * FROM Review WHERE ParentId=@ParentId AND BaseId=0 AND ParentType=" + ParentType + " AND Flag<>" + (int)ReviewFlag.delete;
            SqlParameter para = new SqlParameter("@ParentId", ParentId);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            return dt;
        }
        #endregion

        #region 根据ParentId,ParentType,topNum获取非删除状态的评论信息列表
        /// <summary>
        /// 根据ParentId,ParentType非删除状态的评论信息列表
        /// </summary>
        /// <param name="ParentId">文章或图片的ID</param>
        /// <param name="ParentType">评论的类型</param>
        /// <param name="topNum">需要获取的记录条数</param>
        /// <returns></returns>
        public DataTable GetReviewByParentId(string ParentId, int ParentType, int topNum)
        {
            string sqlString = "SELECT TOP " + topNum + " * FROM Review WHERE ParentId=@ParentId AND BaseId=0 AND ParentType=" + ParentType + " AND Flag<>" + (int)ReviewFlag.delete;
            SqlParameter para = new SqlParameter("@ParentId", ParentId);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            return dt;
        }
        #endregion

        #region 根据ParentId,ParentType获取所有非删除状态的评论
        /// <summary>
        /// 根据ParentId,ParentType获取所有非删除状态的评论
        /// </summary>
        /// <param name="ParentId">需要创建评论Html的文章或图片的ID</param>
        /// <param name="ParentType">评论的类型</param>
        /// <returns></returns>
        public StringBuilder CreateReviewListHtml(string ParentId, int ParentType)
        {
            StringBuilder ReviewListHtml = new StringBuilder();
            DataTable dtReviewList = new ReviewRule().GetReviewByParentId(ParentId, ParentType);
            //如果评论的记录数为空则然会空的StringBuilder对象
            if (dtReviewList.Rows.Count <= 0)
                return new StringBuilder();

            DataTable dtMyReply = new DataTable();
            ReviewListHtml.AppendFormat("<div class=\"title\"><strong>所有评论</strong><span>共有评论<strong>{0}</strong>条</span></div>", dtReviewList.Rows.Count);
            ReviewListHtml.Append("<ul class=\"content\">");
            for (int i = 0; i < dtReviewList.Rows.Count; i++)
            {
                ReviewListHtml.Append("<li>");
                ReviewListHtml.AppendFormat("<div class=\"nick\">{0}<span>{1}</span><em>#{2}</em></div>", dtReviewList.Rows[i]["ReviewerName"].ToString(), dtReviewList.Rows[i]["ReviewTime"].ToString(), i + 1);
                ReviewListHtml.AppendFormat("<p>{0}</p>", dtReviewList.Rows[i]["ReviewContent"].ToString());
                dtMyReply = new ReviewRule().GetReviewByBaseId(dtReviewList.Rows[i]["ID"].ToString(), ParentType);
                foreach (DataRow dr in dtMyReply.Rows)
                {   
                    //我的回复
                    ReviewListHtml.Append("<div class=\"reply\">");
                    ReviewListHtml.AppendFormat("<div class=\"nick\">{0}<span>{1}</span></div>", dr["ReviewerName"].ToString(), dr["ReviewTime"].ToString());
                    ReviewListHtml.AppendFormat("<p>{0}</p>", dr["ReviewContent"].ToString());
                    ReviewListHtml.Append("</div>");
                }
                ReviewListHtml.Append("</li>");
            }
            ReviewListHtml.Append("</ul>");
            return ReviewListHtml;
        }
        #endregion

        #region 根据ParentId,ParentType,topNum获取所有非删除状态的评论
        /// <summary>
        /// 根据ParentId,ParentType,topNum获取所有非删除状态的评论
        /// </summary>
        /// <param name="ParentId">需要创建评论Html的文章或图片的ID</param>
        /// <param name="ParentType">评论的类型</param>
        /// <param name="topNum">需要获取的记录条数</param>
        /// <returns></returns>
        public StringBuilder CreateReviewListHtml(string ParentId, int ParentType, int topNum)
        {
            StringBuilder ReviewListHtml = new StringBuilder();
            DataTable dtReviewList = new ReviewRule().GetReviewByParentId(ParentId, ParentType, topNum);
            //如果评论的记录数为空则然会空的StringBuilder对象
            if (dtReviewList.Rows.Count <= 0)
                return new StringBuilder();

            DataTable dtMyReply = new DataTable();
            ReviewListHtml.Append("<div id=\"review_list\">");
            ReviewListHtml.AppendFormat("<div class=\"title\"><strong>所有评论</strong><span>共有评论<strong id=\"btmCommentCount\">{0}</strong>条</span></div>", dtReviewList.Rows.Count);
            ReviewListHtml.Append("<ul class=\"content\">");
            for (int i = 0; i < dtReviewList.Rows.Count; i++)
            {
                ReviewListHtml.Append("<li>");
                ReviewListHtml.AppendFormat("<div class=\"nick\">{0}<span>{1}</span><em>#{2}</em></div>", dtReviewList.Rows[i]["ReviewerName"].ToString(), dtReviewList.Rows[i]["ReviewTime"].ToString(), i + 1);
                ReviewListHtml.AppendFormat("<p>{0}</p>", dtReviewList.Rows[i]["ReviewContent"].ToString());
                dtMyReply = new ReviewRule().GetReviewByBaseId(dtReviewList.Rows[i]["ID"].ToString(), ParentType);
                foreach (DataRow dr in dtMyReply.Rows)
                {
                    //我的回复
                    ReviewListHtml.Append("<div class=\"reply\">");
                    ReviewListHtml.AppendFormat("<div class=\"nick\">{0}<span>{1}</span></div>", dr["ReviewerName"].ToString(), dr["ReviewTime"].ToString());
                    ReviewListHtml.AppendFormat("<p>{0}</p>", dr["ReviewContent"].ToString());
                    ReviewListHtml.Append("</div>");
                }
                ReviewListHtml.Append("</li>");
            }
            ReviewListHtml.Append("</ul>");
            ReviewListHtml.Append("</div>");
            return ReviewListHtml;
        }
        #endregion

        #region 根据ParentType, topNum获取所有非删除状态的评论
        /// <summary>
        /// 根据ParentType, topNum获取所有非删除状态的评论
        /// </summary>
        /// <param name="ParentType">评论的类型</param>
        /// <param name="topNum">需要获取的记录条数</param>
        /// <returns></returns>
        public DataTable GetAllReviewByParentType(int ParentType, int topNum)
        {
            string sqlString = "SELECT * FROM Review WHERE ParentType=" + ParentType + " AND Flag<>" + (int)ReviewFlag.myAnswer + " AND Flag<>" + (int)ReviewFlag.delete + " ORDER BY ReviewTime DESC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 根据ParentType, topNum获取所有非删除状态的评论
        /// <summary>
        /// 根据ParentType, topNum获取所有评论
        /// </summary>
        /// <param name="ParentType">评论的类型</param>
        /// <param name="topNum">需要获取的记录条数</param>
        /// <returns></returns>
        public StringBuilder CreateReviewListHtml(int ParentType, int topNum)
        {
            StringBuilder ReviewListHtml = new StringBuilder();
            DataTable dtReviewList = new ReviewRule().GetAllReviewByParentType(ParentType, topNum);
            //如果评论的记录数为空则然会空的StringBuilder对象
            //if (dtReviewList.Rows.Count <= 0)
            //    return new StringBuilder();

            DataTable dtMyReply = new DataTable();
            ReviewListHtml.Append("<div id=\"review_list\">");
            ReviewListHtml.AppendFormat("<div class=\"title\"><strong>最近{0}评论</strong></div>", dtReviewList.Rows.Count == 0 ? "没人" : dtReviewList.Rows.Count + "条");
            ReviewListHtml.Append("<ul class=\"content\">");
            for (int i = 0; i < dtReviewList.Rows.Count; i++)
            {
                ReviewListHtml.Append("<li>");
                ReviewListHtml.AppendFormat("<div class=\"nick\">{0}<span>{1}</span><em>#{2}</em></div>", dtReviewList.Rows[i]["ReviewerName"].ToString(), dtReviewList.Rows[i]["ReviewTime"].ToString(), i + 1);
                ReviewListHtml.AppendFormat("<p>{0}</p>", dtReviewList.Rows[i]["ReviewContent"].ToString());
                dtMyReply = new ReviewRule().GetReviewByBaseId(dtReviewList.Rows[i]["ID"].ToString(), ParentType);
                foreach (DataRow dr in dtMyReply.Rows)
                {
                    //我的回复
                    ReviewListHtml.Append("<div class=\"reply\">");
                    ReviewListHtml.AppendFormat("<div class=\"nick\">{0}<span>{1}</span></div>", dr["ReviewerName"].ToString(), dr["ReviewTime"].ToString());
                    ReviewListHtml.AppendFormat("<p>{0}</p>", dr["ReviewContent"].ToString());
                    ReviewListHtml.Append("</div>");
                }
                ReviewListHtml.Append("</li>");
            }
            ReviewListHtml.Append("</ul>");
            ReviewListHtml.Append("</div>");
            return ReviewListHtml;
        }
        #endregion
    }
}
